package com.wen.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.common.Result;
import com.wen.entity.*;
import com.wen.mapper.*;
import com.wen.service.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 陡含含
 * 用户
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class UserController {

    private final MemberService memberService;

    private final UserService userService;

    private final CoachService coachService;

    private final EquipmentService equipmentService;

    private final ConsumRecordService consumRecordService;

    private final CourseService courseService;

    //    添加用户的
    @PostMapping
    public Result<?> save(@RequestBody User user) {
        if (user.getPassword() == null) {
            user.setPassword("123456");
        }
        userService.save(user);
        if (user.getRole() == 2) {
            Member member = new Member();
            member.setUserName(user.getUsername());
            member.setUserId(user.getId());
            member.setBalance(0.00);
            member.setAllMoney(0.00);
            member.setUserId(user.getId());
            memberService.save(member);
        }
        if (user.getRole() == 3) {
            Coach coach = new Coach();
            coach.setUserName(user.getUsername());
            coach.setUserId(user.getId());
            coachService.save(coach);
        }
        return Result.success();
    }

    //    更新
    @PutMapping
    public Result<?> update(@RequestBody User user) {
        userService.updateById(user);
        return Result.success();
    }

    //    删除
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        userService.removeById(id);
        return Result.success();
    }

    //    根据id查询
    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id) {
        User user = userService.getById(id);
        return Result.success(user);
    }

    //    查询
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(User::getUsername, search);
        }
        Page<User> userPage = userService.page(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(userPage);
    }

    //    登录
    @PostMapping("/login")
    public Result<?> login(@RequestBody User user) {
        Map maps = new HashMap();
        //这只是传进来的user，并没有在数据库中进行查找
        User res = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()).eq(User::getPassword, user.getPassword()));
        if (res == null) {
            return Result.error("-1", "用户名或密码错误");
        }
        Member member = memberService.getOne(Wrappers.<Member>lambdaQuery().eq(Member::getUserName, user.getUsername()).eq(Member::getUserId, res.getId()));
        Coach coach = coachService.getOne(Wrappers.<Coach>lambdaQuery().eq(Coach::getUserName, user.getUsername()).eq(Coach::getUserId, res.getId()));


        maps.put("user", res);
        maps.put("member", member);
        maps.put("coach", coach);

        return Result.success(maps);

    }

    //注册
    @PostMapping("/register")
    public Result<?> register(@RequestBody User user) {
        User res = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()));
        if (res != null) {
            return Result.error("-1", "用户名重复");
        }
        if (user.getPassword() == null) {
            user.setPassword("123456");
        }
        userService.save(user);

        if (user.getRole() == 2) {
            Member member = new Member();
            member.setUserName(user.getUsername());
            member.setUserId(user.getId());
            member.setBalance(0.00);
            member.setAllMoney(0.00);
            member.setUserId(user.getId());
            memberService.save(member);
        }
        if (user.getRole() == 3) {
            Coach coach = new Coach();
            coach.setUserName(user.getUsername());
            coach.setUserId(user.getId());
            coachService.save(coach);
        }

        return Result.success();

    }

    @GetMapping("/getCount")
    public Result<?> findMember(@RequestParam String userId) {

        Map map = new HashMap();

        // 教练总数
        Integer count1 = coachService.count();
        map.put("count1", count1);

        // 预约总数
//        LambdaQueryWrapper<MemberOrder> wrapper1 = new LambdaQueryWrapper<>();
//        wrapper1.eq(MemberOrder::getOrderStatus,"1");
//        Integer integer = memberOrderMapper.selectCount(wrapper1);
//        map.put("count2",integer);

        Integer count2 = courseService.count();
        map.put("count2", count2);

        // 设备总数
        List<Equipment> equipment = equipmentService.list();
        int sum = 0;
        if (CollectionUtil.isNotEmpty(equipment)) {
            sum = equipment.stream().mapToInt(Equipment::getCount).sum();
        }
        map.put("count3", sum);

        // 订购课程
        Integer integer1 = consumRecordService.count();
        map.put("count4", integer1);

        return Result.success(map);
    }
}
